import _ from 'lodash';
import i18n from '@src/locales';
import { getRootWindow } from '@src/util/dom';

/* 高级搜索需过滤的字段 */
const FILTER_FIELDS = ['productTemplate', 'linkmanName', 'phone', 'address'];

const BindQrcodeFiled = {
  displayName: i18n.t('product.type.qrcodeState'),
  fieldName: 'qrcodeState',
  formType: 'select',
  export: false,
  isNull: 1,
  isSystem: 1,
  operator: 'between',
  orderId: 1000,
  setting: {
    isMulti: false,
    dataSource: [
      {
        text: i18n.t('common.base.yes'),
        value: 1,
      },
      {
        text: i18n.t('common.base.no'),
        value: 2,
      },
    ],
  },
};

export const matchOperator = field => {
  let formType = field.formType;
  let operator = '';

  switch (formType) {
    case 'date': {
      operator = 'between';
      break;
    }
    case 'datetime': {
      operator = 'between';
      break;
    }
    case 'select': {
      if (field.setting && field.setting.isMulti) {
        operator = 'contain';
      } else {
        operator = 'eq';
      }
      break;
    }
    case 'cascader': {
      operator = 'cascader';
      break;
    }
    case 'user': {
      operator = 'user';
      break;
    }
    case 'address': {
      operator = 'address';
      break;
    }
    case 'location': {
      operator = 'location';
      break;
    }
    case 'related_task': {
      operator = 'array_eq';
      break;
    }
    case 'formula': {
      operator = 'eq';
      break;
    }
    default: {
      operator = 'like';
      break;
    }
  }

  return operator;
};

/**
 * @description: getSelfFields
 * @param {Array} configFields
 * @param {Object} qrcodeEnabled initData?.productConfig?.qrcodeEnabled
 */
export const getSelfFields = (configFields, qrcodeEnabled = false) => {
  let fields = [
    {
      displayName: i18n.t('common.fields.createTime.displayName'),
      fieldName: 'createTime',
      formType: 'datetime',
      export: false,
      isNull: 1,
      isSystem: 1,
      operator: 'between',
      orderId: -2,
    },
    {
      displayName: i18n.t('product.type.hasRemind'),
      fieldName: 'hasRemind',
      formType: 'select',
      export: false,
      isNull: 1,
      isSystem: 1,
      operator: 'between',
      orderId: -3,
      setting: {
        isMulti: false,
        dataSource: [
          {
            text: i18n.t('common.base.yes'),
            value: 1,
          },
          {
            text: i18n.t('common.base.no'),
            value: 0,
          },
        ],
      },
    },
  ];

  // 是否有客户
  let hasCustomer = configFields.find(item => item.formType == 'customer')

  // 华大私有灰度
  const RootWindow = getRootWindow(window)
  const bgiPrivate = Boolean(RootWindow.grayAuth?.bgiPrivate)

  if (bgiPrivate && hasCustomer && hasCustomer.setting.customerOption?.address) {
    fields.push({
      displayName: i18n.t('common.form.type.customerAddress'),
      fieldName: 'productCustomerMultiAddress',
      export: true,
      formType: 'text',
      isSystem: 1,
      tableName: 'product',
      isSearch: 0
    })
  }

  if (qrcodeEnabled) {
    fields.push(BindQrcodeFiled);
  }

  return fields;
};

/**
 * @description: formatFields
 * @param {Array} configFields
 * @param {Object} qrcodeEnabled initData?.productConfig?.qrcodeEnabled
 */
export const formatFields = (configFields, qrcodeEnabled = false) => {
  let f = {};
  let obj = {};
  const selfFields = getSelfFields(configFields, qrcodeEnabled);

  const Fields = [...configFields, ...selfFields]
    .filter(f => f.isSearch || f.isSystem)
    .filter(f => !FILTER_FIELDS.includes(f.fieldName))
    .map(field => {
      f = _.cloneDeep(field);

      let formType = f.formType;

      if (formType === 'datetime') {
        formType = 'date';
      }

      // 产品formType === user只单选
      if (formType === 'user') {
        if(f.setting) {
          f.setting.isMultiple = 0;
          f.setting.isMulti = false;
        } else {
          f.setting = { 
            isMultiple: 0,
            isMulti: false,
          }
        }
      }

      if (f.fieldName === 'customer') {
        formType = 'customer';
      }

      if (f.fieldName === 'createUser') {
        if(f.setting) {
          f.setting.isMulti = false
        } else {
          f.setting = { isMulti: false }
        }
      }

      if (formType === 'updateTime') {
        f.displayName = i18n.t('common.fields.updateTime.displayName');
      }
      if (formType == 'related_catalog' && f.tableName == 'product') {
        // 转换产品类型表单数据
        f.fieldName = 'catalogId';
      }

      // TODO: 需要确认修改后有没有问题
      if (f.displayName === i18n.t('common.base.serviceDepartment')) {
        formType = 'tags';
        if(f.setting) {
          f.setting.isMulti = false
        } else {
          f.setting = { isMulti: false }
        }
      }

      return Object.assign({
        ...f,
        isNull: 1,
        formType,
        originalFormType: f.formType,
        operator: matchOperator(f),
      });
    })
    .sort((a, b) => a.orderId - b.orderId)
    .reduce((item, next) => {
      // 过滤掉相同fieldName的字段 去重
      obj[next.fieldName] ? '' : obj[next.fieldName] = true && item.push(next);
      return item;
    }, []);

  return Fields;
};

// 客户系统字段
export const customerSystemFields = [
  {
    label: i18n.t('common.label.customerTag'),
    displayName: i18n.t('common.label.customerTag'),
    fieldName: 'customerTag',
    field: 'customerTag',
    tableName: 'customer',
    isSystem: true,
    fixed: true,
    show: true,
    export: true,
  },
]